package com.jms.framework.beans;

import com.jms.framework.beans.annotation.Autowired;
import com.jms.framework.exception.DataNotLegitimateException;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * GeneralBeanDefinition类是AbstractBeanDefinition的子类，用于定义和管理Bean的元数据
 * 它提供了更详细的Bean配置信息，如依赖注入、初始化和销毁方法
 * @author JMS
 * @since 20250103
 * @version 1.0.0
 */
public class GeneralBeanDefinition extends AbstractBeanDefinition {

    /**
     * 构造函数用于初始化Bean的定义
     *
     * @param beanName Bean的名称
     * @param beanClass Bean的类
     * @param scope Bean的作用域，只能是SCOPE_PROTOTYPE或SCOPE_SINGLETON
     * @param lazyInit 是否懒初始化
     * @param orderNumber Bean的加载顺序
     * @param afterInit 是否在初始化之后执行
     *
     * @throws DataNotLegitimateException 如果作用域不合法，则抛出此异常
     */
    public GeneralBeanDefinition(String beanName, Class<?> beanClass, String scope, boolean lazyInit, int orderNumber, boolean afterInit) {
        // 检查作用域是否合法
        if (!Objects.equals(scope, BeanDefinition.SCOPE_PROTOTYPE) && !Objects.equals(scope, BeanDefinition.SCOPE_SINGLETON)) {
            throw new DataNotLegitimateException("bean scope is not legal.");
        }
        // 初始化Bean的名称、作用域和懒初始化属性
        this.beanName = beanName;
        this.scope = scope;
        this.lazyInit = lazyInit;
        beanClassName = beanClass.getName();
        this.orderNumber = orderNumber;
        this.afterInit = afterInit;

        // 获取Bean类的所有方法
        Method [] methods = beanClass.getMethods();
        List<BeanDependency> beanDependencies = new LinkedList<>();
        for (Method method : methods) {
            // 处理PostConstruct注解，设置初始化方法
            if (method.isAnnotationPresent(PostConstruct.class)) {
                if (initMethodName != null) {
                    throw new DataNotLegitimateException("bean init method should only one.");
                }
                initMethodName = method.getName();
            }
            // 处理PreDestroy注解，设置销毁方法
            if (method.isAnnotationPresent(PreDestroy.class)) {
                if (destroyMethodName != null) {
                    throw new DataNotLegitimateException("bean destroy method should only one.");
                }
                destroyMethodName = method.getName();
            }
            // 处理Autowired注解，收集依赖注入信息
            if (method.isAnnotationPresent(Autowired.class)) {
                Autowired autowired = method.getAnnotation(Autowired.class);
                String dependencyName = autowired.value();
                Class<?> dependencyClass = method.getParameterTypes()[0];
                if ("".equals(dependencyName)) {
                    dependencyName = null;
                }
                beanDependencies.add(new BeanDependency(dependencyName, dependencyClass.getName(), method));
            }
        }
        // 将收集的依赖信息转换为数组并赋值给dependencies属性
        dependencies = beanDependencies.toArray(new BeanDependency[0]);
    }
}
